package graduation.dao.Impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import graduation.dao.MeetingDao;
import graduation.dto.MeetingDto;
import graduation.entity.Meeting;


@Repository
public class MeetingDaoImpl extends HibernateDaoSupport implements MeetingDao{
	@Autowired  
    public void setSessionFactoryOverride(SessionFactory sessionFactory)  
    {  
        super.setSessionFactory(sessionFactory);  
    }
	
	@Resource
	private SessionFactory sessionFactory;

	private Session getSession() {
		return sessionFactory.getCurrentSession();
	}
	/**
	 * 添加会议
	 */
	@Override
	public void add(Meeting meeting) {
		// TODO Auto-generated method stub
		Session session = this.getSession();
		session.save(meeting);
	}
	/*
	 * 通过id删除(non-Javadoc)
	 * @see graduation.dao.MeetingDao#deleteById(int)
	 */
	@Override
	public void deleteById(int id) {
		// TODO Auto-generated method stub
		Session session = this.getSession();
		//查询
		Query query = session.createQuery("from Meeting where id=?");
        //2、填写上一步中占位符的内容
        query.setParameter(0, id);
        Meeting meeting = (Meeting)query.uniqueResult();
        //删除
        session.delete(meeting);
	}
	/**
	 * 查询全部用户
	 */
	@Override
	public List<Meeting> getAllMeeting() {
		// TODO Auto-generated method stub
		Session session = this.getSession();
		String hql = "from Meeting m order by m.time desc";
		Query query = session.createQuery(hql);
		List<MeetingDto> list = query.list();
		System.out.println("MeetingDto"+list);
		return query.list();
	}
	

}
